// Copyright (C) 2025 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include "test.hpp"

#include <rothar/pegtl/contrib/trace.hpp>

namespace rothar
{
   using GRAMMAR1 = sor< failure, one< 'a' > >;
   using GRAMMAR2 = seq< one< 'a' >, any, any, any, any, one< 'b' >, eof >;
   using GRAMMAR3 = sor< one< 'a' >, one< 'b' > >;
#if defined( __cpp_exceptions )
   using GRAMMAR4 = try_catch< sor< one< 'a' >, must< one< 'b' > > > >;
#endif

   template< typename Rule >
   struct trace_action
   {};

   unsigned a0 = 0;
   unsigned a = 0;

   template<>
   struct trace_action< one< 'a' > >
   {
      template< typename... Ts >
      static void apply0( Ts&&... /*unused*/ )
      {
         ++a0;
      }
   };

   template<>
   struct trace_action< GRAMMAR1 >
   {
      template< typename... Ts >
      static void apply( Ts&&... /*unused*/ )
      {
         ++a;
      }
   };

   void unit_test()
   {
      {
         memory_input in( "ab", "trace test please ignore" );
         const auto result = standard_trace< GRAMMAR1 >( in );
         ROTHAR_PEGTL_TEST_ASSERT( result );
         ROTHAR_PEGTL_TEST_ASSERT( a0 == 0 );
         ROTHAR_PEGTL_TEST_ASSERT( a == 0 );
      }
      {
         memory_input in( "ab", "trace test please ignore" );
         const auto result = standard_trace< GRAMMAR1, trace_action >( in );
         ROTHAR_PEGTL_TEST_ASSERT( result );
         ROTHAR_PEGTL_TEST_ASSERT( a0 == 1 );
         ROTHAR_PEGTL_TEST_ASSERT( a == 1 );
      }
      {
         memory_input in( "a\r\n\t\0b", 6, "trace test please ignore" );
         const auto result = standard_trace< GRAMMAR2 >( in );
         ROTHAR_PEGTL_TEST_ASSERT( result );
         ROTHAR_PEGTL_TEST_ASSERT( a0 == 1 );
         ROTHAR_PEGTL_TEST_ASSERT( a == 1 );
      }
      {
         memory_input in( "a\r\n\t\0b", 6, "trace test please ignore" );
         const auto result = standard_trace< GRAMMAR2, trace_action >( in );
         ROTHAR_PEGTL_TEST_ASSERT( result );
         ROTHAR_PEGTL_TEST_ASSERT( a0 == 2 );
         ROTHAR_PEGTL_TEST_ASSERT( a == 1 );
      }
      {
         memory_input in( "c", "trace test please ignore" );
         const auto result = standard_trace< GRAMMAR3 >( in );
         ROTHAR_PEGTL_TEST_ASSERT( !result );
      }
#if defined( __cpp_exceptions )
      {
         memory_input in( "c", "trace test please ignore" );
         const auto result = standard_trace< GRAMMAR4 >( in );
         ROTHAR_PEGTL_TEST_ASSERT( !result );
      }
#endif
   }

}  // namespace rothar

#include "main.hpp"
